/*
 * This file is part of the Sx Framework Library.
 * 
 * Copyright (C) 2013 University of Colorado Denver
 * <min.choi@ucdenver.edu> <shane.transue@ucdenver.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
 * copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */
#include <sxDirectX_11_Context.h>

/* DirectX 11 Includes */
#define WIN32_LEAN_AND_MEAN
#include <d3d11.h>
#include <dxgi.h>

Sx::DirectX::Graphics::Contexts::DirectX_11_Context::DirectX_11_Context() {
	this->device = nullptr;
	this->deviceContext = nullptr;
	this->factory = nullptr;
	this->adapter = nullptr;
	this->dxgiDevice = nullptr;

	this->initialize();
}

Sx::DirectX::Graphics::Contexts::DirectX_11_Context::~DirectX_11_Context() {
	if ( this->factory ) this->factory->Release();
	if ( this->adapter ) this->adapter->Release();
	if ( this->dxgiDevice ) this->dxgiDevice->Release();
	if ( this->deviceContext ) this->deviceContext->Release();
	if ( this->device ) this->device->Release();
}

bool Sx::DirectX::Graphics::Contexts::DirectX_11_Context::initialize() {
	this->initialized = true;
	return this->initializeDevice();
}

bool Sx::DirectX::Graphics::Contexts::DirectX_11_Context::initializeDevice() {
	//--------------------------------------------------------------------------
	// Create the DirectX Device
	//--------------------------------------------------------------------------
	HRESULT deviceResult = D3D11CreateDevice(nullptr,
									   D3D_DRIVER_TYPE_HARDWARE,
									   nullptr,
									   D3D11_CREATE_DEVICE_DEBUG,
									   nullptr,
									   0,
									   D3D11_SDK_VERSION,
									   &this->device,
									   nullptr,
									   &this->deviceContext);

	if ( deviceResult == E_INVALIDARG ) {
		std::cout << "[DirectX_11_Context:initializeDevice] Error: An invalid parameter was passed to the returning function." << std::endl;
		std::cout << "  DirectX device not initialized." << std::endl;
		this->device = nullptr;
		this->deviceContext = nullptr;
		return false;
	}

	if ( deviceResult == E_FAIL ) {
		std::cout << "[DirectX_11_Context:initializeDevice] Error: Attempted to create a device with the debug layer enabled and the layer is not installed." << std::endl;
		std::cout << "  DirectX device not initialized." << std::endl;
		this->device = nullptr;
		this->deviceContext = nullptr;
		return false;
	}

	if ( deviceResult != S_OK ) {
		std::cout << "[DirectX_11_Context:initializeDevice] Error: Failed creating DirectX device." << std::endl;
		std::cout << "  DirectX device not initialized." << std::endl;
		this->device = nullptr;
		this->deviceContext = nullptr;
		return false;
	}

	//--------------------------------------------------------------------------
	// Initialize DXGI device and factory.
	//--------------------------------------------------------------------------
	this->device->QueryInterface(&this->dxgiDevice);
	dxgiDevice->GetAdapter(&this->adapter);
	this->adapter->GetParent(IID_PPV_ARGS(&this->factory));
	return true;
}

bool Sx::DirectX::Graphics::Contexts::DirectX_11_Context::preRender() const {
	return true;
}

bool Sx::DirectX::Graphics::Contexts::DirectX_11_Context::postRender() const {
	return true;
}

ID3D11DeviceContext* const Sx::DirectX::Graphics::Contexts::DirectX_11_Context::getDeviceContext() const {
	return this->deviceContext;
}

ID3D11Device* const Sx::DirectX::Graphics::Contexts::DirectX_11_Context::getDevice() const {
	return this->device;
}

IDXGIFactory1* const Sx::DirectX::Graphics::Contexts::DirectX_11_Context::getFactory() const {
	return this->factory;
}

IDXGIAdapter* const Sx::DirectX::Graphics::Contexts::DirectX_11_Context::getAdapter() const {
	return this->adapter;
}

IDXGIDevice* const Sx::DirectX::Graphics::Contexts::DirectX_11_Context::getDXGIDevice() const {
	return this->dxgiDevice;
}
